package com.meixiang.dialog;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.activity.homes.shopping.ShoppingCartActivity;
import com.meixiang.activity.homes.shopping.VerifyGoodsOrderActivity;
import com.meixiang.adapter.shopping.SelectSettleStoreAdapter;
import com.meixiang.entity.shopping.CartBalanceStoreEntity;
import com.meixiang.entity.shopping.result.BalanceAccountResult;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.util.AbJsonUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 选择结算的店铺
 *
 * @author LZRUI
 */
public class SelectSettleStoreDialog extends BaseDialogFragment implements SelectSettleStoreAdapter.SelectStoreListener {

    public static final String TAG = "SelectSettleStoreDialog";
    private static final String LIST = "list";

    @Bind(R.id.ry_store)
    RecyclerView ryStore;
    private List<CartBalanceStoreEntity> storeList;
    private SelectSettleStoreAdapter adapter;

    public static SelectSettleStoreDialog newInstance(ArrayList<CartBalanceStoreEntity> storeList) {
        SelectSettleStoreDialog dialog = new SelectSettleStoreDialog();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(LIST, storeList);
        dialog.setArguments(bundle);
        return dialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            storeList = getArguments().getParcelableArrayList(LIST);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    @Override
    protected int setFragmentViewId() {
        return R.layout.dialog_select_settle_store;
    }

    @Override
    protected void initView(View rootView) {
        ButterKnife.bind(this, rootView);
        ryStore.setLayoutManager(new LinearLayoutManager(mActivity));
        ryStore.setAdapter(adapter = new SelectSettleStoreAdapter(mActivity, storeList));
        adapter.setSelectStoreListener(this);
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.tv_close)
    @Override
    public void onClick(View v) {
        dismiss(); // 关闭
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void toBalance(String cartIds) {
        HttpUtils.post(Config.BALANCE_ACCOUNT, ShoppingCartActivity.TAG, new HttpParams("cartIds", cartIds), new HttpCallBack(mActivity) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                BalanceAccountResult balanceAccountResult = AbJsonUtil.fromJson(result.toString(), BalanceAccountResult.class);
                Intent intent = new Intent(mActivity, VerifyGoodsOrderActivity.class);
                intent.putExtra(VerifyGoodsOrderActivity.ORDER_DATA, balanceAccountResult);
                mActivity.startActivity(intent);
                dismiss();
            }

            @Override
            public void onError(String code, String message) {
                Toast.makeText(mActivity, message, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
